package com.baosight.hdsdk.example;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.tuple.ImmutablePair;

import com.baosight.hdjni.HDErrcode;
import com.baosight.hdsdk.HDConnectionFactory;
import com.baosight.hdsdk.HDDataProvider;
import com.baosight.hdsdk.HDDataWriter;
import com.baosight.hdsdk.HDTagManager;
import com.baosight.hdsdk.HDServerFactory;
import com.baosight.hdsdk.common.HDBasicTagPropMask;
import com.baosight.hdsdk.common.HDTagClass;
import com.baosight.hdsdk.common.HDTagDataType;
import com.baosight.hdsdk.domain.data.HDBasicTag;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDDataServer;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;

public class testQuery {

	public static void main(String[] args) {
		System.out.println("####################");
		System.out.println(System.getProperty("java.library.path"));

		String serverIP = "10.25.25.129";		
		String tagName = "hdsys_netin_0";
		
		HDDataServer server = HDServerFactory.getHDDataServer(serverIP,5673,"",5673);
		HDDataConnection dataConn = null;
		
		try {
			dataConn = HDConnectionFactory.getHDDataConnection(server);
			dataConn.loginToServer("admin", "admin");
		} catch (HDSdkException e) {
			e.printStackTrace();
		}
		
		HDDataWriter dw = new HDDataWriter(dataConn);
		HDDataProvider dataProvider = new HDDataProvider(dataConn); 
		
		int tagID = 0;		
		HDTagManager tm;
		HDBasicTag tag = null;
		tm = dataConn.getTagManager();
		
		//getTag		
		try{
			tag = tm.getBasicTagByName(tagName);
		}catch(HDSdkException e){
			e.printStackTrace();
		}
		tagID = tag.getId();
		tag.getTagClass();
		System.out.println("tagid:");
		System.out.println(tagID);

		//save record
		HDRecord record = new HDRecord();
		Date now = new Date();		
		/*		record.setSecond(now.getTime() / 1000);
		record.setQuality(192);
		record.setValueStr("abc");				
		try{
			dw.saveSnapshot(tagID, record);
		}catch(HDSdkException e){
			e.printStackTrace();
		}*/
		 
		List<Integer> tagIDs = new ArrayList<Integer>();
		tagIDs.add(tagID);
		List<ImmutablePair<HDRecord, Integer>> recordList = null;
		HDErrcode errall = new HDErrcode();
			
		try{
			recordList = dataProvider.querySnapshotsByIDs(tagIDs, errall);
			record = recordList.get(0).left;
			System.out.println("value:");
			System.out.println(record.getValueStr());
			System.out.println(record.getTimeStampStr());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		System.out.println("HDTagDataType:");
		System.out.println(tag.getTagDataType());
		if (tag.getTagDataType() == HDTagDataType.BLOB)
		{			
			String str = record.getValueStr();
			byte[] byteStr;
			try {
				byteStr = str.getBytes("ISO-8859-1");
				int size = str.length()/2;
				short[] shortArray = new short[size];
				for (int index = 0; index < size; index++)
				{
					shortArray[index] = (short) (byteStr[2*index+1]<<8 | byteStr[2*index] & 0xFF);
					ByteBuffer bb = ByteBuffer.allocate(2);
					bb.order(ByteOrder.LITTLE_ENDIAN);
					bb.put(byteStr[2*index]);
					bb.put(byteStr[2*index+1]);
					System.out.println(bb.getShort(0));
				}
			} catch (UnsupportedEncodingException e) {
				
				e.printStackTrace();
			}
			System.out.println(record.getTimeStampStr());
		} 
	}
}
